package com.barnett.tool;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.provider.MediaStore;

public class MusicTool {
	// 播放对象
	private MediaPlayer myMediaPlayer;
	private OnCompletionListener onCompletionListener;
	private OnMusicToolListener onMusicToolListener;

	/** Called when the activity is first created. */
	public MusicTool() {
		myMediaPlayer = new MediaPlayer();
		onCompletionListener = new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				if (onMusicToolListener != null) {
					onMusicToolListener.onCompletion(mp);
				}
			}
		};
		handler.post(updateThread);
	}

	private Handler handler = new Handler();
	private Runnable updateThread = new Runnable() {
		public void run() {
			// 获得歌曲现在播放位置并设置成播放进度条的值
			if (myMediaPlayer != null) {
				if (onMusicToolListener != null) {
					onMusicToolListener.setProgress(myMediaPlayer
							.getCurrentPosition());
				}
				// 每次延迟100毫秒再启动线程
				handler.postDelayed(updateThread, 100);
			}
		}
	};

	/**
	 * 设置音乐播放完毕的监听
	 */
	public void setOnMusicToolListener(OnMusicToolListener onMusicToolListener) {
		this.onMusicToolListener = onMusicToolListener;
	}

	/**
	 * 设置播放器进度
	 * 
	 * @param progress
	 */
	public void setPorgress(int progress) {
		if (myMediaPlayer != null) {
			myMediaPlayer.seekTo(progress);
		}
	}

	/**
	 * 播放音乐
	 * 
	 * @param path
	 */
	public void playMusic(String path) {
		try {
			myMediaPlayer.reset();
			myMediaPlayer.setDataSource(path);
			myMediaPlayer.prepare();
			myMediaPlayer.start();
			myMediaPlayer.seekTo(0);
			if (onMusicToolListener != null) {
				onMusicToolListener.onStart(myMediaPlayer.getDuration());
			}
			if (onCompletionListener != null) {
				myMediaPlayer.setOnCompletionListener(onCompletionListener);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	/**
	 * 停止
	 */
	public void stop() {
		// TODO Auto-generated method stub
		if (myMediaPlayer.isPlaying()) {
			myMediaPlayer.seekTo(0);
			myMediaPlayer.stop();
		}
	}

	/**
	 * 暂停
	 */
	public void pause() {
		if (myMediaPlayer.isPlaying()) {
			myMediaPlayer.pause();
		} else {
			myMediaPlayer.start();
		}
	}

	/**
	 * 关闭
	 */
	public void onDestroy() {
		myMediaPlayer.stop();
		myMediaPlayer.release();
		myMediaPlayer = null;
	}

	/**
	 * 搜索媒体库
	 * 
	 * @param ctx
	 * @return
	 */
	public Cursor queryMedia(Context ctx) {
		ContentResolver resolver = ctx.getContentResolver();
		if (resolver == null) {
			return null;
		}
		return resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
		/* 这个字符串数组表示要查询的列 */
		new String[] { MediaStore.Video.Media.TITLE, // 音乐名
				MediaStore.Audio.Media.DURATION, // 音乐的总时间
				MediaStore.Audio.Media.ARTIST, // 艺术家
				MediaStore.Audio.Media._ID, // id号
				MediaStore.Audio.Media.DISPLAY_NAME, // 音乐文件名
				MediaStore.Audio.Media.DATA // 音乐文件的路径
				}, null, null, null);
	}

	/**
	 * 监听器
	 * 
	 * @author jiang
	 */
	public interface OnMusicToolListener {
		public void onStart(int max);// 设置初始的最大值

		public void setProgress(int progress);// 设置进度监听

		public void onCompletion(MediaPlayer mp);// 监听音乐结束
	}
	/**
	 * 提升音量
	 * @param context
	 */
	public void setStreamVolumeUp(Context context) {
		AudioManager am = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		am.adjustStreamVolume(AudioManager.STREAM_MUSIC,
				AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
	}
	/**
	 * 降低音量
	 * @param context
	 */
	public void setStreamVolumeDown(Context context) {
		AudioManager am = (AudioManager) context
				.getSystemService(Context.AUDIO_SERVICE);
		am.adjustStreamVolume(AudioManager.STREAM_MUSIC,
				AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
	}
}